<?php

namespace Yolo\Rpc;

use Yolo\Errors\RpcException;
use Yolo\Service\RpcResponse;

/**
 * Rpc
 * @date 2024-12-10
 */
class Rpc
{
    /**
     * Return an error response
     * @param RpcException $e
     * @param array|null $metadata
     * @return RpcException
     * @deprecated DO NOT use it, it will remove in the future
     */
    public static function error(RpcException $e, ?array $metadata = null): RpcException
    {
        if ($metadata !== null) {
            $e->addMetadata($metadata);
        }

        return $e;
    }

    /**
     * Return an ok response
     * @return array
     */
    public static function ok(): array
    {
        return [
            'message' => 'ok',
        ];
    }

    /**
     * Return a list response
     * @param int $totalRows
     * @param array $data
     * @return array
     */
    public static function list(int $totalRows, array $data): array
    {
        return [
            'totalRows' => $totalRows,
            'data' => $data,
        ];
    }

    /**
     * Return a resource created response
     * @param RpcResponse $response
     * @return RpcResponse
     */
    public static function created(RpcResponse $response): RpcResponse
    {
        $response->setStatusCode(201);
        $response->setBody(json_encode(self::ok()));
        return $response;
    }
}
